<?php
/**
 * Created by PhpStorm.
 * Author: 赵凯
 * Date: 2020/2/4
 * Time: 16:47
 */

namespace api\task\controller;


use api\common\model\RlEtAzdModel;
use api\common\model\RlEtAzsModel;
use api\common\model\RlEtCustomerModel;
use api\common\model\RlHiseServiceProjectModel;
use Mpdf\Mpdf;
use think\Db;
use think\Controller;
use think\Log;
use api\common\service\MsgService;

class AutopdfController extends Controller
{
    public function generatePdf(){
        set_time_limit(0);
        $path_dir = '/pdf/'.date('Ymd');

        if (!file_exists('..'.$path_dir)){
            mkdir ('..'.$path_dir,0777,true);
        }
//
//        $pdf = New Pdf("/usr/bin/wkhtmltopdf");
//
//        $service_project_id = RlHiseServiceProjectModel::where('azd_id',$azd_id)->field('hise_service_project_id')->find()['hise_service_project_id'];
        $list = RlHiseServiceProjectModel::Where('wgbgnr','')->where('gczt',5)->limit(30)->field('azd_id')->select()->toarray();

        if(!$list){
            Log::write(date('Y-m-d h:i:s',time()).'没有可生成pdf的工程');
            return false;
        }
        foreach($list as $valone){
            $path = $path_dir.'/'.time().rand(1,10).'.pdf';
            $res['wgbgnr'] =$path;
            $res['sfscysbg'] =1;
            $path ='..'.$path;

//        $pdf->generate('http://rixcx.akng.net/pdf/pdf/pdf?service_project_id='.$service_project_id,$path);
//        return 1;
//        $path ='../'.'pdf/'.date('Yhd').'/'.date('his').rand(1,9).'.pdf';
//        $azd_id ='JZXCX201912191101397796';
            $service_project_id = RlHiseServiceProjectModel::where('azd_id',$valone['azd_id'])->field('hise_service_project_id')->find()['hise_service_project_id'];
            $data = $this->PrepositionReport($service_project_id);
            $name = $data['info']['yh_name'];
            $xb = $data['info']['xb']==1 ? '男' : '女';
            $address_detail = $data['info']['address_detail'];
            $address = $data['info']['address'];
            $phone = $data['info']['sj_one'];
            $wagshj = date('Y-m-d',$data['info']['wgsj']);
            $config = [
                'mode' => 'utf-8'];
            $mpdf = new Mpdf($config);
            $mpdf->autoScriptToLang = true;
            $mpdf->autoLangToFont = true;
            $html = "<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf-8' />

		<meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no'/>
        <link rel='stylesheet' href='css/reset.css' />
        <link rel='stylesheet' href='css/main.css' />
        <style type='text/css'>
          .install_top{color: #FFFFFF;}
          table,tr,th,td,div{padding: 0;margin: 0;box-sizing: border-box;}
          .text_th{
            text-align: left;
            font-weight: normal;
            height: 40px;
            line-height: 40px;
            background: #EFF3F9;
            padding: 0 15px;
            font-size: 15px;
          }
          .text_th .img{
            display: inline-block;
            max-width: 18px;
            max-height: 16px;
            margin-right: 8px;
            margin-bottom: 3px;
          }
          .input_table td{
            border-bottom: 1px solid #eeeeee;
            color: #666666;
          }
          .input_table .label{
            line-height: 54px;
            font-size: 15px;
          }
          .input_table .in_left{
            width: 110px;
            padding-left: 15px;
            height: 54px;
            line-height: 54px;
          }
          .input_table .mainform{
            color: #666666;
          }
          .input_table .moreh .label{
            line-height: 76px;
          }
          .input_table .moreh .mainform{
            height: 76px;
          }
          .recode{
            padding: 0 15px;
          }
          .install_tb{
            padding-left: 15px;
            padding-right: 15px;
            width: 100%;
            margin-top: 20px;
            margin-bottom: 25px;
          }
          .install_tb th{
            font-weight: normal;
            font-size: 13px;
          }
          .install_tb .titleo{
            height: 40px;
          }
          .install_tb .in_thr{
            text-align: right;
          }
          .install_tb img{
            max-width: 100%;
          }
          .recode .re_li .time{
            float: none;
            padding-left: 0;
          }
          .recode .re_li .imglist .ili{
            float: none;
            width: 33.3333%;
          }
          .recode .re_li .imglist .ili .iimg{
            width: 50%;
          }
          .install_tb .in_thl{
            text-align: left;
          }
          .install_tb .in_thr{
            text-align: right;
            width: 115px;
          }
        </style>
	</head>
	<body>
		<div class='container'>
		<div class='water_top'> </div>
			<div class='install_top'>工程报告书</div>
      <table class='install_box input_table'>
        <tr>
          <th colspan='2' class='text_th'>
            <div class='title'>
            	<img src='images/icon-installtop.png' class='img' />业主信息
            </div>
          </th>
        </tr>
        <tr>
          <td class='in_left'><div class='label'>姓名</div></td>
          <td>
            <div class='mainform'>
            	<div class='input'>$name</div>
            </div>
          </td>
        </tr>
        <tr>
          <td class='in_left'><div class='label'>性别</div></td>
          <td>
            <div class='mainform'>
            	<div class='input'>$xb</div>
            </div>
          </td>
        </tr>
        <tr>
          <td class='in_left'><div class='label'>手机</div></td>
          <td>
            <div class='mainform'>
            	<div class='input'>$phone</div>
            </div>
          </td>
        </tr>
        <tr>
          <td class='in_left'><div class='label'>所在地区</div></td>
          <td>
            <div class='mainform'>
            	<div class='input'>$address</div>
            </div>
          </td>
        </tr>
        <tr class='moreh'>
          <td class='in_left'><div class='label'>详细地址</div></td>
          <td>
            <div class='mainform'>
            	<div class='input'>$address_detail</div>
            </div>
          </td>
        </tr>
      </table>
			
			<div class='install_box'>
				<div class='title'>
					<img src='images/icon-installtop3.png' class='img' />安装记录
				</div>
				<div class='recode'>
					<table class='install_tb re_li'>";
            foreach ($data['node_list'] as $k =>$v){
                $html .= "<tr class='titleo'><th class='in_thl'><div class='txt'>";
                $html  .=  $v['node_name'];
                $html  .= "</div></th><th></th><th class='in_thr'><div class='time'><img src='images/icon-time-red.png' class='img' /> ";
                $html  .= date("Y-m-d ",$v['jdqrsj']);
                $html  .= "</div></th>";
                $html  .= "</tr><tr class='imglist'><td class='ili'><img src=";
                if($v['tp_one']) {
                    $html  .="'.." . $v['tp_one']."'";
                }else{
                    $html  .='';
                }
                $html  .=" class='iimg' style='width: 98px;height: 98px;margin-top: 15px;margin-bottom: 20px;'></td>";
                $html  .= "<td class='ili'><img src=";
                if($v['tp_two']) {
                    $html  .="'.." . $v['tp_two']."'";
                }else{
                    $html  .='';
                }
                $html  .=" class='iimg' style='width: 98px;height: 98px;margin-top: 15px;margin-bottom: 20px;'></td>";
                $html  .= "<td class='ili'><img src=";
                if($v['tp_there']) {
                    $html  .="'.." . $v['tp_there']."'";
                }else{
                    $html  .='';
                }
                $html  .=" class='iimg' style='width: 98px;height: 98px;margin-top: 15px;margin-bottom: 20px;'></td>";
                $html  .= "</tr> ";
            }
            $html  .="</table>
			</div>
			<div class='install_box'>
				<div class='title'>
					<img src='images/icon-installtop4.png' mode='aspectFit' class='img' />调试结论
				</div>
				<div class='bgwhite'>
					<div class='bugs'>
						<img src='images/icon-selected.png' mode='aspectFit' class='img' /> 调试合格
					</div>
				</div>
			</div>
			<div class='install_box'>
				<div class='title'>
					<img src='images/icon-installtop6.png' mode='aspectFit' class='img'>
					用户须知
				</div>
				<div class='bgwhite'>
					<div class='instructions'>
						<div class='txt'>在您使用空调过程中需要注意以下几点事项：</div>
						<table class='attention-tb op'>
			              <tr class='p'>
			                <td><img src='images/icon-intro1.png' mode='aspectFit' class='img'/></td>
			                <td>
			                  <div class='lo' style='margin-left: 15px;'>
			                   <span> 待机耗电：</span>因为空调在不运转的情况下存在待机耗电，如果长时间不用空调，可切断空调电源；
			                  </div>
			                </td>
			              </tr>
			              <tr class='p'>
			                <td><img src='images/icon-intro2.png' mode='aspectFit' class='img'/></td>
			                <td>
			                  <div class='lo' style='margin-left: 15px;'>
			                   <span> 预热保护：</span>长时间断电后，需要使用空调时，请提前一天通电；
			                  </div>
			                </td>
			              </tr>
			              <tr class='p'>
			                <td><img src='images/icon-intro3.png' mode='aspectFit' class='img'/></td>
			                <td>
			                  <div class='lo' style='margin-left: 15px;'>
			                   <span> 清洗保养：</span>空调使用时间较长时，会导致灰尘堆积到过滤网上，影响效果和增大噪音，建议定期清洗过滤网。
			                  </div>
			                </td>
			              </tr>
			            </table>
						<div class='time'>$wagshj</div>
					</div>
					<div class='hot_phone'><img src='images/icon-phone2.png' class='icon' /> 安装服务监督热线：4006111111-7</div>
				</div>
			</div>

		</div>
	</body>
</html>";
            $mpdf->WriteHTML($html);
            $mpdf->Output($path,"f");
          RlHiseServiceProjectModel::Where('azd_id',$valone['azd_id'])->update($res);
          if(config('installsend9'))
          {
	        // 引用发送验证码类
	            $msg_send = new MsgService();
	            // 发送手机验证码
	            $msgres = $msg_send->installsend9($service_project_id,$phone);
		        if($msgres)
		        {
			        trace($phone.'||'.date('Y-m-d h:i:s',time()).'交付短信发送成功','info');
		        }else{
		        	trace($phone.'||'.date('Y-m-d h:i:s',time()).'交付短信发送失败','info');
		        }
          }
        }
        Log::write(date('Y-m-d h:i:s',time()).'生成pdf的成功');
        return true;

    }

    //报告书前置
    public function  PrepositionReport($service_project_id)
    {
        //多表引入查询方法
        $info = RlHiseServiceProjectModel::Where('hise_service_project_id',$service_project_id)->field('azd_id,yh_name,xb,sj_one,provice,city,qx,address,qyfgs,jxs
        ,azs,azd,fgsfwfzr,cplb,yyjc_sj,jkh,zjbxq,zybjbxq,jxsxsy,bz,wgsj,pic_one,pic_two')->find();
//        if(!$info) $this->error('该用户不存在');
        if(empty($info['qx'])){
            $city = Db::name('rl_hise_dq_table')->where('dq_code',$info['city'])->field('dq_name')->select()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code',$info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice.$city.$info['address'];
            $info['address'] = $provice.$city;
        }else{
            $xq = Db::name('rl_hise_qx_table')->where('qx_code',$info['qx'])->field('qx_name')->find()['qx_name'];
            $city = Db::name('rl_hise_dq_table')->where('dq_code',$info['city'])->field('dq_name')->find()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code',$info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice.$city.$xq.$info['address'];
            $info['address'] = $provice.$city.$xq;
        }
        $info['jxs_name'] = RlEtCustomerModel::where('jxsbm',$info['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $info['azs_name'] = RlEtAzsModel::where('server_code',$info['azs'])->field('server_name')->find()['server_name'];
        $info['azd_name'] = RlEtAzdModel::where('engineer_code',$info['azd'])->field('engineer_name')->find()['engineer_name'];
        $info['fgs_name'] = Db::name('rl_company')->where('orgdept',$info['qyfgs'])->field('orgname')->find()['orgname'];
        $info['yyjc_sj']  = date('Y-m-d', $info['yyjc_sj']);
        $info['jxs_name'] = RlEtCustomerModel::where('jxsbm',$info['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $prefix = 'cmf_';
        $join = [
            ["{$prefix}rl_node node", 'node.node_id = pro.azjd'],
        ];
        $list = Db::name('rl_hise_service_pro_process')->alias('pro')
            ->join($join)
            ->where('pro.gc_azdid',$info['azd_id'])->order('node.sort desc')
            ->field('pro.jdqrsj,pro.cplp,pro.azjd,pro.tp_one,pro.tp_two,pro.tp_there,node.node_name')
            ->select();
        $data['info'] = $info;
        $data['node_list'] = $list;
        return $data;
    }

}